import { defineStore } from 'pinia'
import { getHomeData } from '@/api/home'

export const useHomeStore = defineStore('home', {
  state: () => ({
    featuredProblems: [],
    featuredArticles: []
  }),
  actions: {
    async fetchHomeData() {
      try {
        const response = await getHomeData()
        this.featuredProblems = response.featuredProblems.map(p => ({
          id: p.id,
          title: p.title,
          difficulty: p.difficulty,
          acceptanceRate: p.acceptanceRate,
          problemId: p.problemId,
          problemTitle: p.problemTitle
        }))
        
        this.featuredArticles = response.featuredArticles.map(log => ({
            id: log.id,
            title: log.title,
            date: log.date,
            preview: log.preview,
            likes: log.likes,
            view_count: log.viewCount,
            author: {
              email: log.author.email,
              name: log.author.name,
              avatar: log.author.avatar
            }
        }))
      } catch (error) {
        console.error('获取首页数据失败:', error)
      }
    }
  }
})
